////#import "RatingInfoView.h"
////#import <Masonry/Masonry.h>
////
////#define kIconSize 30.0f                  // 图标大小
////#define kGoodRateFontSize 24.0f          // 好评率字体大小
////#define kDescFontSize 12.0f              // 描述文字字体大小
////#define kCategoryFontSize 12.0f          // 分类字体大小
////#define kHorizontalPadding 15.0f         // 水平间距
////#define kVerticalPadding 12.0f           // 垂直间距
////#define kInterItemSpacing 8.0f           // 项目间距
////#define kCornerRadius 8.0f               // 圆角半径
////
////@interface RatingInfoView ()
////
////@property (nonatomic, strong) UIImageView *iconImageView;
////@property (nonatomic, strong) UILabel *rateLabel;
////@property (nonatomic, strong) UILabel *rateDescLabel;
////@property (nonatomic, strong) UIStackView *categoryStackView;
////@property (nonatomic, strong) NSArray<NSString *> *categoryTitles;
////@property (nonatomic, strong) NSMutableArray<UIButton *> * categoryButtons;
////
////@end
////
////@implementation RatingInfoView
////
////- (instancetype)initWithFrame:(CGRect)frame {
////    return [self initWithFrame:frame categoryTitles:@[@"全部", @"好评", @"中评", @"有图", @"差评"]];
////}
////
////- (instancetype)initWithFrame:(CGRect)frame categoryTitles:(NSArray<NSString *> *)categoryTitles {
////    self = [super initWithFrame:frame];
////    if (self) {
////        _categoryTitles = categoryTitles;
////        _categoryButtons = [NSMutableArray array];
////        _selectedIndex = 0;
////        [self setupUI];
////        [self setupConstraints];
////        [self setupDefaultStyles];
////    }
////    return self;
////}
////
////- (void)setupUI {
////    // 图标视图
////    _iconImageView = [[UIImageView alloc] init];
////    _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
////    _iconImageView.image = [UIImage systemImageNamed:@"face.smiling"];
////    [self addSubview:_iconImageView];
////    
////    // 好评率标签
////    _rateLabel = [[UILabel alloc] init];
////    _rateLabel.font = [UIFont systemFontOfSize:kGoodRateFontSize];
////    _rateLabel.textColor = kColorHEX(0xFF6B32);
////    [self addSubview:_rateLabel];
////    
////    // 好评率描述标签
////    _rateDescLabel = [[UILabel alloc] init];
////    _rateDescLabel.font = [UIFont systemFontOfSize:kDescFontSize];
////    _rateDescLabel.textColor = kColorHEX(0x666666);
////    _rateDescLabel.text = @"好评率";
////    [self addSubview:_rateDescLabel];
////    
////    // 分类堆栈视图
////        _categoryStackView = [[UIStackView alloc] init];
////        _categoryStackView.axis = UILayoutConstraintAxisHorizontal;
////        _categoryStackView.alignment = UIStackViewAlignmentCenter;
////        _categoryStackView.distribution = UIStackViewDistributionFillEqually;
////        _categoryStackView.spacing = kInterItemSpacing;
////        [self addSubview:_categoryStackView];
////        
////        // 创建分类按钮
////        for (NSInteger i = 0; i < _categoryTitles.count; i++) {
////            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
////            button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
////            button.titleLabel.textAlignment = NSTextAlignmentCenter;
////            button.titleLabel.numberOfLines = 2;
////            button.tag = i;
////            [button addTarget:self action:@selector(categoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
////            [_categoryStackView addArrangedSubview:button];
////            [_categoryButtons addObject:button];
////        }
////}
////
////- (void)setupConstraints {
////    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.left.equalTo(self).offset(kHorizontalPadding);
////        make.centerY.equalTo(self);
////        make.size.mas_equalTo(CGSizeMake(kIconSize, kIconSize));
////    }];
////    
////    [_rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.left.equalTo(_iconImageView.mas_right).offset(kHorizontalPadding / 2);
////        make.top.equalTo(_iconImageView);
////    }];
////    
////    [_rateDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.top.equalTo(_rateLabel.mas_bottom);
////        make.centerX.equalTo(_rateLabel);
////    }];
////    
////    [_categoryStackView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.left.equalTo(self).offset(kHorizontalPadding);
////        make.right.equalTo(self).offset(-kHorizontalPadding);
////        make.top.equalTo(_iconImageView.mas_bottom).offset(kVerticalPadding);
////        make.bottom.equalTo(self).offset(-kVerticalPadding);
////    }];
////}
////
////- (void)setupDefaultStyles {
////    self.backgroundColor = [UIColor whiteColor];
//////    self.layer.cornerRadius = kCornerRadius;
//////    self.layer.masksToBounds = YES;
//////    self.layer.borderWidth = 0.5;
//////    self.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0].CGColor;
////    
////    _goodRateTextColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0];
////    _categoryTextColor = [UIColor colorWithRed:0.2 green:0.6 blue:0.9 alpha:1.0];
////}
////
////- (void)updateViewData {
////    // 更新好评率
////    _rateLabel.text = [NSString stringWithFormat:@"%.0f%%", _goodRate * 100];
////    _rateLabel.textColor = _goodRateTextColor ?: [UIColor systemRedColor];
////    
////    // 更新分类标签
////    NSArray *counts = @[@(_allCount), @(_goodCount), @(_mediumCount), @(_picCount), @(_badCount)];
////    
////    for (NSInteger i = 0; i < _categoryButtons.count; i++) {
////        UIButton *label = _categoryButtons[i];
////        NSInteger count = [counts[i] integerValue];
////        
////        if (count >= 10000) {
////            [label setTitle:[NSString stringWithFormat:@"%@\n%.1f万+", _categoryTitles[i], count / 10000.0] forState:UIControlStateNormal];
//////            label.titleLabel.text = [NSString stringWithFormat:@"%@\n%.1f万+", _categoryTitles[i], count / 10000.0];
////        } else {
////            [label setTitle:[NSString stringWithFormat:@"%@\n%d+", _categoryTitles[i], (int)count] forState:UIControlStateNormal];
////
//////            label.titleLabel.text = [NSString stringWithFormat:@"%@\n%d+", _categoryTitles[i], (int)count];
////        }
////        
//////        label.textColor = _categoryTextColor ?: [UIColor systemBlueColor];
////    }
////    
////    [self setNeedsLayout];
////}
////
////- (void)layoutSubviews {
////    [super layoutSubviews];
////    [self updateViewData];
////}
////
////+ (CGFloat)calculateHeightForWidth:(CGFloat)width {
////    // 计算内容高度
////    CGFloat contentHeight = kIconSize + 2 * kVerticalPadding + 40; // 图标高度 + 上下间距 + 分类区域高度
////    return contentHeight;
////}
////
////@end
//
//
//
//#import "RatingInfoView.h"
//#import <Masonry/Masonry.h>
//
//#define kIconSize 30.0f                  // 图标大小
//#define kGoodRateFontSize 24.0f          // 好评率字体大小
//#define kDescFontSize 14.0f              // 描述文字字体大小
//#define kCategoryFontSize 12.0f          // 分类字体大小
//#define kHorizontalPadding 15.0f         // 水平间距
//#define kVerticalPadding 12.0f           // 垂直间距
//#define kInterItemSpacing 8.0f           // 项目间距
//#define kInterRowSpacing 12.0f           // 行间距
//#define kCornerRadius 8.0f               // 圆角半径
//#define kSelectedScale 1.1f              // 选中时的缩放比例
//
//@interface RatingInfoView ()
//
//@property (nonatomic, strong) UIImageView *iconImageView;
//@property (nonatomic, strong) UILabel *rateLabel;
//@property (nonatomic, strong) UILabel *rateDescLabel;
//@property (nonatomic, strong) NSMutableArray<UIButton *> *categoryButtons;
//@property (nonatomic, strong) NSArray<NSString *> *categoryTitles;
//
//@end
//
//@implementation RatingInfoView
//
//- (instancetype)initWithFrame:(CGRect)frame {
//    return [self initWithFrame:frame categoryTitles:@[@"全部", @"好评", @"中评", @"有图", @"差评"]];
//}
//
//- (instancetype)initWithFrame:(CGRect)frame categoryTitles:(NSArray<NSString *> *)categoryTitles {
//    self = [super initWithFrame:frame];
//    if (self) {
//        _categoryTitles = categoryTitles;
//        _categoryButtons = [NSMutableArray array];
//        _selectedIndex = 0;          // 默认选中"全部"
//        _maxItemsPerRow = 3;         // 默认每行最多3个项目
//        
//        [self setupUI];
//        [self setupConstraints];
//        [self setupDefaultStyles];
//    }
//    return self;
//}
//
//- (void)setupUI {
//    // 图标视图
//    _iconImageView = [[UIImageView alloc] init];
//    _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
//    _iconImageView.image = [UIImage systemImageNamed:@"face.smiling"];
//    [self addSubview:_iconImageView];
//    
//    // 好评率标签
//    _rateLabel = [[UILabel alloc] init];
//    _rateLabel.font = [UIFont boldSystemFontOfSize:kGoodRateFontSize];
//    [self addSubview:_rateLabel];
//    
//    // 好评率描述标签
//    _rateDescLabel = [[UILabel alloc] init];
//    _rateDescLabel.font = [UIFont systemFontOfSize:kDescFontSize];
//    _rateDescLabel.text = @"好评率";
//    [self addSubview:_rateDescLabel];
//    
//    // 创建分类按钮
//    for (NSInteger i = 0; i < _categoryTitles.count; i++) {
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//        button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
//        button.titleLabel.textAlignment = NSTextAlignmentCenter;
//        button.titleLabel.numberOfLines = 2;
//        button.tag = i;
//        [button addTarget:self action:@selector(categoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:button];
//        [_categoryButtons addObject:button];
//    }
//}
//
//- (void)setupConstraints {
//    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).offset(kHorizontalPadding);
//        make.top.equalTo(self).offset(kVerticalPadding);
//        make.size.mas_equalTo(CGSizeMake(kIconSize, kIconSize));
//    }];
//    
//    [_rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_iconImageView.mas_right).offset(kHorizontalPadding / 2);
//        make.centerY.equalTo(_iconImageView);
//    }];
//    
//    [_rateDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_rateLabel.mas_right).offset(kHorizontalPadding / 4);
//        make.centerY.equalTo(_rateLabel);
//    }];
//    
//    // 分类按钮的约束将在layoutSubviews中根据实际位置设置
//}
//
//- (void)setupDefaultStyles {
//    self.backgroundColor = [UIColor whiteColor];
//    self.layer.cornerRadius = kCornerRadius;
//    self.layer.masksToBounds = YES;
//    self.layer.borderWidth = 0.5;
//    self.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0].CGColor;
//    
//    _goodRateTextColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0];
//    _normalTextColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
//    _selectedTextColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0]; // 选中为红色
//}
//
//- (void)updateViewData {
//    // 更新好评率
//    _rateLabel.text = [NSString stringWithFormat:@"%.0f%%", _goodRate * 100];
//    _rateLabel.textColor = _goodRateTextColor;
//    
//    // 更新分类按钮
//    NSArray *counts = @[@(_allCount), @(_goodCount), @(_mediumCount), @(_picCount), @(_badCount)];
//    
//    for (NSInteger i = 0; i < _categoryButtons.count; i++) {
//        UIButton *button = _categoryButtons[i];
//        NSInteger count = [counts[i] integerValue];
//        
//        if (count >= 10000) {
//            [button setTitle:[NSString stringWithFormat:@"%@\n%.1f万+", _categoryTitles[i], count / 10000.0] forState:UIControlStateNormal];
//        } else {
//            [button setTitle:[NSString stringWithFormat:@"%@\n%d+", _categoryTitles[i], (int)count] forState:UIControlStateNormal];
//        }
//        
//        // 设置选中状态
//        BOOL isSelected = (i == _selectedIndex);
//        [button setTitleColor:isSelected ? _selectedTextColor : _normalTextColor forState:UIControlStateNormal];
//        button.titleLabel.font = isSelected ? [UIFont boldSystemFontOfSize:kCategoryFontSize] : [UIFont systemFontOfSize:kCategoryFontSize];
//        
//        // 添加选中效果
//        if (isSelected) {
//            button.transform = CGAffineTransformMakeScale(kSelectedScale, kSelectedScale);
//        } else {
//            button.transform = CGAffineTransformIdentity;
//        }
//    }
//    
//    [self setNeedsLayout];
//}
//
//- (void)categoryButtonTapped:(UIButton *)sender {
//    [self setSelectedIndex:sender.tag animated:YES];
//    
//    // 通知代理
//    if ([self.delegate respondsToSelector:@selector(ratingInfoView:didSelectCategoryAtIndex:)]) {
//        [self.delegate ratingInfoView:self didSelectCategoryAtIndex:sender.tag];
//    }
//}
//
//- (void)setSelectedIndex:(NSInteger)index animated:(BOOL)animated {
//    if (_selectedIndex == index) {
//        return;
//    }
//    
//    _selectedIndex = index;
//    
//    if (animated) {
//        [UIView animateWithDuration:0.2 animations:^{
//            [self updateViewData];
//        }];
//    } else {
//        [self updateViewData];
//    }
//}
//
//- (void)layoutSubviews {
//    [super layoutSubviews];
//    
//    // 计算按钮大小和位置
//    CGFloat contentWidth = self.bounds.size.width - 2 * kHorizontalPadding;
//    CGFloat buttonWidth = (contentWidth - (_maxItemsPerRow - 1) * kInterItemSpacing) / _maxItemsPerRow;
//    CGFloat buttonHeight = 40; // 按钮高度固定
//    
//    // 布局分类按钮
//    for (NSInteger i = 0; i < _categoryButtons.count; i++) {
//        UIButton *button = _categoryButtons[i];
//        NSInteger row = i / _maxItemsPerRow;
//        NSInteger col = i % _maxItemsPerRow;
//        
//        CGFloat x = kHorizontalPadding + col * (buttonWidth + kInterItemSpacing);
//        CGFloat y = kVerticalPadding + kIconSize + kVerticalPadding + row * (buttonHeight + kInterRowSpacing);
//        
//        button.frame = CGRectMake(x, y, buttonWidth, buttonHeight);
//    }
//    
//    [self updateViewData];
//}
//
//+ (CGFloat)calculateHeightForWidth:(CGFloat)width categoryCount:(NSInteger)count maxItemsPerRow:(NSInteger)maxItems {
//    NSInteger rows = (count + maxItems - 1) / maxItems;
//    CGFloat contentHeight = kIconSize + 2 * kVerticalPadding + (rows - 1) * kInterRowSpacing + 40 * rows;
//    return contentHeight;
//}
//+ (CGFloat)calculateHeightForWidth:(CGFloat)width {
//    // 计算内容高度
//    CGFloat contentHeight = kIconSize + 2 * kVerticalPadding + 40; // 图标高度 + 上下间距 + 分类区域高度
//    return contentHeight;
//}
//
//@end

#define kIconSize 30.0f                  // 图标大小
#define kGoodRateFontSize 24.0f          // 好评率字体大小
#define kDescFontSize 14.0f              // 描述文字字体大小
#define kCategoryFontSize 12.0f          // 分类字体大小
#define kHorizontalPadding 15.0f         // 水平间距
#define kVerticalPadding 12.0f           // 垂直间距
#define kInterItemSpacing 8.0f           // 项目间距
#define kInterRowSpacing 12.0f           // 行间距
#define kCornerRadius 8.0f               // 圆角半径
#define kSelectedScale 1.1f              // 选中时的缩放比例
//
#import "RatingInfoView.h"
#import <Masonry/Masonry.h>
#import "XMCommentViewCell.h"
#define kOverallRatingWidth 80.0f       // 总体评分区域宽度
#define kItemHeight 60.0f               // 项目高度
#define kTitleFontSize 14.0f            // 标题字体大小
#define kRatingFontSize 16.0f           // 评分字体大小
#define kSelectedScale 1.05f            // 选中时的缩放比例

@interface RatingInfoView ()

@property (nonatomic, strong) UILabel *overallTitleLabel;    // 总体标题标签
@property (nonatomic, strong) UILabel *overallRatingLabel;   // 总体评分标签
@property (nonatomic, strong) NSArray<NSString *> *ratingTypes;  // 评价类型
@property (nonatomic, strong) NSMutableArray<UIButton *> *typeButtons;  // 类型按钮
//@property (nonatomic, strong) id<RatingInfoViewDelegate> delegate;  // 代理


@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *totalReviewsLabel;
@property (nonatomic, strong) UIView *reviewTypesContainerView;


@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *rateLabel;
@property (nonatomic, strong) UILabel *rateDescLabel;
@property (nonatomic, strong) UIStackView *categoryStackView;
@property (nonatomic, strong) NSArray<NSString *> *categoryTitles;
@property (nonatomic, strong) NSMutableArray<UIButton *> * categoryButtons;
@end

@implementation RatingInfoView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
////        _ratingTypes = ratingTypes;
//        _typeButtons = [NSMutableArray array];
//        _selectedIndex = 0;
//        _overallRating = 5.0;
//        _overallTitle = @"总体评分";
//        _backgroundColor = [UIColor whiteColor];
//        _titleTextColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
//        _ratingTextColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0];
//        _selectedTextColor = [UIColor whiteColor];
//        _borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
//        _borderWidth = 0.5;
//        _cornerRadius = 8.0;
//        _itemSpacing = 5.0;
     self.dataArray = @[@"mine_add_friend_icon", @"mine_avatar_icon", @"mine_deal_browseRecord_icon",
        @"mine_deal_meetbox_icon", @"mine_deal_mymeet_icon", @"mine_deal_myorder_icon",
        @"mine_detail_icon", @"mine_scan_icon", @"mine_set_icon"];
        [self setupUI];
//        [self initSetupUI];
    }
    return self;
}

- (void)setupUI {
//    self.backgroundColor = _backgroundColor;
//    self.layer.cornerRadius = _cornerRadius;
//    self.layer.masksToBounds = YES;
//    
//    // 创建总体评分区域
//    _overallTitleLabel = [[UILabel alloc] init];
//    _overallTitleLabel.font = [UIFont systemFontOfSize:kTitleFontSize];
//    _overallTitleLabel.textColor = _titleTextColor;
//    _overallTitleLabel.textAlignment = NSTextAlignmentCenter;
//    _overallTitleLabel.text = _overallTitle;
//    [self addSubview:_overallTitleLabel];
//    
//    _overallRatingLabel = [[UILabel alloc] init];
//    _overallRatingLabel.font = [UIFont boldSystemFontOfSize:kRatingFontSize];
//    _overallRatingLabel.textColor = _ratingTextColor;
//    _overallRatingLabel.textAlignment = NSTextAlignmentCenter;
//    _overallRatingLabel.text = [NSString stringWithFormat:@"%.1f", _overallRating];
//    [self addSubview:_overallRatingLabel];
//    
//    // 创建类型按钮
//    for (NSInteger i = 0; i < _ratingTypes.count; i++) {
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.titleLabel.font = [UIFont systemFontOfSize:kTitleFontSize];
//        button.titleLabel.textAlignment = NSTextAlignmentCenter;
//        button.titleLabel.numberOfLines = 2;
//        button.tag = i;
//        [button addTarget:self action:@selector(typeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:button];
//        [_typeButtons addObject:button];
//        
//        // 设置初始状态
//        [button setTitleColor:_titleTextColor forState:UIControlStateNormal];
//        button.backgroundColor = [UIColor clearColor];
//        button.layer.borderWidth = _borderWidth;
//        button.layer.borderColor = _borderColor.CGColor;
//    }
//    
//    [self updateButtonStates];
    
    
    
    [self setupHeaderUI];
    
    
    
    // 创建布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout = layout;
   
    // 创建 CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                                     collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
//    self.collectionView.backgroundColor = kColorHEX(0xF9F9F9);
    self.collectionView.backgroundColor = [UIColor clearColor];

    [self.collectionView registerClass:[XMCommentViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    [self addSubview:self.collectionView];
}
- (void)setupHeaderUI {
    // 图标视图
    _iconImageView = [[UIImageView alloc] init];
    _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    _iconImageView.image = [UIImage systemImageNamed:@"face.smiling"];
    [self addSubview:_iconImageView];

    // 好评率标签
    _rateLabel = [[UILabel alloc] init];
    _rateLabel.font = [UIFont boldSystemFontOfSize:kGoodRateFontSize];
    [self addSubview:_rateLabel];

    // 好评率描述标签
    _rateDescLabel = [[UILabel alloc] init];
    _rateDescLabel.font = [UIFont systemFontOfSize:kDescFontSize];
    _rateDescLabel.text = @"好评率";
    [self addSubview:_rateDescLabel];
    [self setupConstraints];
}

- (void)setupConstraints {
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kHorizontalPadding);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kIconSize, kIconSize));
    }];

    [_rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).offset(kHorizontalPadding / 2);
        make.centerY.equalTo(_iconImageView);
    }];

    [_rateDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_rateLabel.mas_right).offset(kHorizontalPadding / 4);
        make.centerY.equalTo(_rateLabel);
    }];

    // 分类按钮的约束将在layoutSubviews中根据实际位置设置
}
//
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMCommentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // 配置 Cell（示例：添加图片）
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
//    imageView.image = [UIImage imageNamed:self.dataArray[indexPath.item]];
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
//    imageView.clipsToBounds = YES;
//    [cell.contentView addSubview:imageView];
    
    return cell;
}



#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 计算单个元素的尺寸
    CGFloat availableWidth = collectionView.bounds.size.width; // 可用宽度（减去左右边距）
    CGFloat itemWidth = (availableWidth - (ColumnCount - 1) * ItemSpacing) / ColumnCount; // 单个元素宽度
    return CGSizeMake(70, 30); // 假设九宫格为正方形
}

//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    return UIEdgeInsetsMake(EdgeInsets, EdgeInsets, EdgeInsets, EdgeInsets); // 四周内边距
//}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 布局总体评分区域
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    _overallTitleLabel.frame = CGRectMake(0, 0, kOverallRatingWidth, height / 2);
    _overallRatingLabel.frame = CGRectMake(0, height / 2, kOverallRatingWidth, height / 2);
    self.collectionView.frame = CGRectMake(KScaleWidth(150), KScaleWidth(10), KScaleWidth(250), height - 10);
//    // 计算每行每列的项目数
//    NSInteger totalItems = _ratingTypes.count;
//    NSInteger rows = (totalItems + 2) / 3; // 向上取整
//    NSInteger cols = MIN(totalItems, 3);
//    
//    // 计算项目大小
//    CGFloat itemWidth = (width - kOverallRatingWidth - (cols - 1) * _itemSpacing) / cols;
//    
//    // 布局类型按钮
//    for (NSInteger i = 0; i < totalItems; i++) {
//        NSInteger row = i / 3;
//        NSInteger col = i % 3;
//        
//        CGFloat x = kOverallRatingWidth + col * (itemWidth + _itemSpacing);
//        CGFloat y = row * (kItemHeight + _itemSpacing);
//        
//        UIButton *button = _typeButtons[i];
//        button.frame = CGRectMake(x, y, itemWidth, kItemHeight);
//    }
}

- (void)setRatingCounts:(NSArray<NSNumber *> *)counts {
    for (NSInteger i = 0; i < MIN(counts.count, _typeButtons.count); i++) {
        NSInteger count = [counts[i] integerValue];
        NSString *title = [NSString stringWithFormat:@"%@\n%ld", _ratingTypes[i], (long)count];
        [_typeButtons[i] setTitle:title forState:UIControlStateNormal];
    }
}

- (void)setSelectedIndex:(NSInteger)index animated:(BOOL)animated {
    if (index >= 0 && index < _typeButtons.count && _selectedIndex != index) {
        _selectedIndex = index;
        
        if (animated) {
            [UIView animateWithDuration:0.2 animations:^{
                [self updateButtonStates];
            }];
        } else {
            [self updateButtonStates];
        }
        
        // 通知代理
        if ([self.delegate respondsToSelector:@selector(ratingGrid:didSelectTypeAtIndex:)]) {
            [self.delegate ratingGrid:self didSelectTypeAtIndex:index];
        }
    }
}

- (void)typeButtonTapped:(UIButton *)sender {
    [self setSelectedIndex:sender.tag animated:YES];
}

- (void)updateButtonStates {
    for (NSInteger i = 0; i < _typeButtons.count; i++) {
        UIButton *button = _typeButtons[i];
        BOOL isSelected = (i == _selectedIndex);
        
        if (isSelected) {
            [button setTitleColor:_selectedTextColor forState:UIControlStateNormal];
            button.backgroundColor = _ratingTextColor;
            button.transform = CGAffineTransformMakeScale(kSelectedScale, kSelectedScale);
        } else {
            [button setTitleColor:_titleTextColor forState:UIControlStateNormal];
            button.backgroundColor = [UIColor clearColor];
            button.transform = CGAffineTransformIdentity;
        }
    }
}
- (void)initSetupUI {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 8.0;
    self.layer.masksToBounds = YES;
    
    // 标题和总评数字段
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"商品评价";
    _titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    _titleLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    [self addSubview:_titleLabel];
    
    _totalReviewsLabel = [[UILabel alloc] init];
    _totalReviewsLabel.text = @"共36万+条评价";
    _totalReviewsLabel.font = [UIFont systemFontOfSize:12.0];
    _totalReviewsLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [self addSubview:_totalReviewsLabel];
    
    // 评价类型容器（九宫格）
    _reviewTypesContainerView = [[UIView alloc] init];
    [self addSubview:_reviewTypesContainerView];
    
    // 设置约束
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(15);
    }];
    
    [_totalReviewsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(17);
        make.right.equalTo(self).offset(-15);
    }];
    
    [_reviewTypesContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(15);
        make.left.right.bottom.equalTo(self);
    }];
}

- (void)updateWithData:(NSDictionary *)data {
    // 清除现有评价类型按钮
    for (UIView *view in _reviewTypesContainerView.subviews) {
        [view removeFromSuperview];
    }
    
    // 更新总评数字段
    _totalReviewsLabel.text = [NSString stringWithFormat:@"共%@条评价", data[@"totalReviews"]];
    
    // 获取评价类型数据
    NSArray *reviewTypes = data[@"reviewTypes"];
    if (!reviewTypes || reviewTypes.count == 0) {
        return;
    }
    
    // 计算每个格子的大小
    CGFloat cellWidth = self.bounds.size.width / 3;
    CGFloat cellHeight = 80.0; // 每个格子的高度
    
    // 创建九宫格评价类型按钮
    for (NSInteger i = 0; i < reviewTypes.count; i++) {
        NSDictionary *typeData = reviewTypes[i];
        NSInteger row = i / 3;
        NSInteger col = i % 3;
        
        // 创建评价类型按钮
        UIButton *typeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        typeButton.backgroundColor = [UIColor whiteColor];
        typeButton.tag = i;
        [typeButton addTarget:self action:@selector(reviewTypeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_reviewTypesContainerView addSubview:typeButton];
        
        // 设置按钮约束
        [typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_reviewTypesContainerView).offset(col * cellWidth  + 100);
            make.top.equalTo(_reviewTypesContainerView).offset(row * cellHeight);
            make.width.mas_equalTo(cellWidth);
            make.height.mas_equalTo(cellHeight);
        }];
        
        // 添加数量标签
        UILabel *countLabel = [[UILabel alloc] init];
        countLabel.text = typeData[@"count"];
        countLabel.font = [UIFont boldSystemFontOfSize:16.0];
        countLabel.textAlignment = NSTextAlignmentCenter;
        countLabel.textColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0];
        [typeButton addSubview:countLabel];
        
        // 添加类型标签
        UILabel *typeLabel = [[UILabel alloc] init];
        typeLabel.text = typeData[@"type"];
        typeLabel.font = [UIFont systemFontOfSize:12.0];
        typeLabel.textAlignment = NSTextAlignmentCenter;
        typeLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
        [typeButton addSubview:typeLabel];
        
        // 设置标签约束
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(typeButton).offset(15);
            make.centerX.equalTo(typeButton);
        }];
        
        [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(countLabel.mas_bottom).offset(5);
            make.centerX.equalTo(typeButton);
        }];
        
        // 添加分隔线
        if (col < 2) { // 不是最右边的按钮
            UIView *verticalLine = [[UIView alloc] init];
            verticalLine.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
            [typeButton addSubview:verticalLine];
            
            [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(typeButton);
                make.top.bottom.equalTo(typeButton).inset(10);
                make.width.mas_equalTo(0.5);
            }];
        }
        
        if (row < (reviewTypes.count - 1) / 3) { // 不是最下边的按钮
            UIView *horizontalLine = [[UIView alloc] init];
            horizontalLine.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
            [typeButton addSubview:horizontalLine];
            
            [horizontalLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(typeButton);
                make.left.right.equalTo(typeButton);
                make.height.mas_equalTo(0.5);
            }];
        }
    }
}

#pragma mark - Action Methods

- (void)reviewTypeButtonTapped:(UIButton *)sender {
    NSArray *reviewTypes = [self.reviewData valueForKeyPath:@"reviewTypes.type"];
    if (sender.tag < reviewTypes.count && [self.delegate respondsToSelector:@selector(orderReviewsView:didSelectReviewType:)]) {
        [self.delegate orderReviewsView:self didSelectReviewType:reviewTypes[sender.tag]];
    }
}
@end
